/**
 * Copyright 2012-2013 University Of Southern California
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.workflowsim.examples.planning;

import java.io.File;
import java.util.Calendar;
import java.util.List;
import org.cloudbus.cloudsim.Log;
import org.cloudbus.cloudsim.core.CloudSim;
import org.workflowsim.CondorVM;
import org.workflowsim.WorkflowDatacenter;
import org.workflowsim.Job;
import org.workflowsim.WorkflowEngine;
import org.workflowsim.WorkflowPlanner;
import org.workflowsim.utils.ClusteringParameters;
import org.workflowsim.utils.OverheadParameters;
import org.workflowsim.utils.Parameters;
import org.workflowsim.utils.ReplicaCatalog;

/**
 * This DHEFTPlanningAlgorithmExample1 creates a workflow planner, a workflow
 * engine, and one schedulers, one data centers and 20 heterogeneous vms that
 * has different communication cost (such that HEFT algorithm should work)
 * 这个DHEFTPlanningAlgorithmExample1（DHEFT调度算法）创建一个工作流规程序,工作流 
 * 引擎和一个调度器,一个数据中心和20异构vm 
 * 有不同的通信成本(分量算法应该工作)
 * @author Weiwei Chen
 * @since WorkflowSim Toolkit 1.1
 * @date Nov 9, 2013
 */
public class DHEFTPlanningAlgorithmExample1 extends HEFTPlanningAlgorithmExample1 {

    ////////////////////////// STATIC METHODS 静态方法///////////////////////
    /**
     * Creates main() to run this example This example has only one datacenter
     * and one storage
	 *创建main()方法来运行这个例子本例中只有一个数据中心 
	 *和一个存储
     */
    public static void main(String[] args) {

        try {
            // First step: Initialize the WorkflowSim package. 
        	//初始化WorkflowSim包
            /**
             * However, the exact number of vms may not necessarily be vmNum If
             * the data center or the host doesn't have sufficient resources the
             * exact vmNum would be smaller than that. Take care.
             * 然而,虚拟机的具体数量可能不一定是vmNum如果 
			 * 数据中心或主机没有足够的资源 
			 * 具体的虚拟机数量将小于vmNum。注意。
             */
            int vmNum = 5;//number of vms虚拟机数目;
            /**
             * Should change this based on real physical path
             * 应该根据真实的物理路径改变这个路径
             */
           // String daxPath = "/Users/weiweich/NetBeansProjects/WorkflowSim-1.0/config/dax/Montage_100.xml";
            String daxPath = "config/dax/Montage_100.xml";
            File daxFile = new File(daxPath);
            if (!daxFile.exists()) {
                Log.printLine("Warning: Please replace daxPath with the physical path in your working environment!");
                return;
            }

            /**
             * Since we are using HEFT planning algorithm, the scheduling
             * algorithm should be static such that the scheduler would not
             * override the result of the planner
			 * 由于我们使用分量规划算法、调度 
			 * 算法应该是静态的,调度器将不会 
			 * 覆盖规划的结果
             */
            Parameters.SchedulingAlgorithm sch_method = Parameters.SchedulingAlgorithm.STATIC;
            Parameters.PlanningAlgorithm pln_method = Parameters.PlanningAlgorithm.DHEFT;
            ReplicaCatalog.FileSystem file_system = ReplicaCatalog.FileSystem.LOCAL;

            /**
             * No overheads
             * 无开销
             */
            OverheadParameters op = new OverheadParameters(0, null, null, null, null, 0);

            /**
             * No Clustering
             * 无集群
             */
            ClusteringParameters.ClusteringMethod method = ClusteringParameters.ClusteringMethod.NONE;
            ClusteringParameters cp = new ClusteringParameters(0, 0, method, null);

            /**
             * Initialize static parameters
             * 初始化静态参数
             */
            Parameters.init(vmNum, daxPath, null,
                    null, op, cp, sch_method, pln_method,
                    null, 0);
            ReplicaCatalog.init(file_system);

            // before creating any entities.在创建任何实体之前.
            int num_user = 1;   // number of grid users网格用户的数量
            Calendar calendar = Calendar.getInstance();
            boolean trace_flag = false;  // mean trace events意思是跟踪事件

            // Initialize the CloudSim library初始化CloudSim库
            CloudSim.init(num_user, calendar, trace_flag);

            WorkflowDatacenter datacenter0 = createDatacenter("Datacenter_0");

            /**
             * Create a WorkflowPlanner with one schedulers.
             * 创建一个拥有一个调度器的工作流规划程序
             */
            WorkflowPlanner wfPlanner = new WorkflowPlanner("planner_0", 1);
            /**
             * Create a WorkflowEngine.
             * 创建一个工作流引擎
             */
            WorkflowEngine wfEngine = wfPlanner.getWorkflowEngine();
            /**
             * Create a list of VMs.The userId of a vm is basically the id of
             * the scheduler that controls this vm.
             * 创建一个虚拟机列表。一个vm的userId是基本的id这个虚拟机调度程序控制。
             */
            List<CondorVM> vmlist0 = createVM(wfEngine.getSchedulerId(0), Parameters.getVmNum());

            /**
             * Submits this list of vms to this WorkflowEngine.
             * 提交这个虚拟机列表给工作流引擎
             */
            wfEngine.submitVmList(vmlist0, 0);

            /**
             * Binds the data centers with the scheduler.
             * 绑定的数据中心调度器
             */
            wfEngine.bindSchedulerDatacenter(datacenter0.getId(), 0);

            CloudSim.startSimulation();
            List<Job> outputList0 = wfEngine.getJobsReceivedList();
            CloudSim.stopSimulation();
            printJobList(outputList0);
        } catch (Exception e) {
            e.printStackTrace();
            Log.printLine("The simulation has been terminated due to an unexpected error");
        }
    }
}
